package top.heliming.youyou.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.heliming.youyou.service.GoodsHtmlService;

/**
 * description: //TODO
 *
 * @author: heliming
 * @date:2019/06/07 下午 2:44
 */
@Component
public class GoodsListener {
  @Autowired
  private GoodsHtmlService goodsHtmlService;

  @RabbitListener(bindings = @QueueBinding(
      value = @Queue(value = "leyou.create.web.queue", durable = "true"),
      exchange = @Exchange(
          value = "leyou.item.exchange",
          ignoreDeclarationExceptions = "true",
          type = ExchangeTypes.TOPIC),
      key = {"item.insert", "item.update"}))
  public void listenCreate(Long id) throws Exception {
    if (id == null) {
      return;
    }
    //新增或修改
    goodsHtmlService.createHtml(id);
  }

  @RabbitListener(bindings = @QueueBinding(
      value = @Queue(value = "leyou.delete.web.queue", durable = "true"),
      exchange = @Exchange(
          value = "leyou.item.exchange",
          ignoreDeclarationExceptions = "true",
          type = ExchangeTypes.TOPIC),
      key = "item.delete"))
  public void listenDelete(Long id) {
    if (id == null) {
      return;
    }
    //删除
    goodsHtmlService.deleteHtml(id);
  }
}
